#include "message_handlers.h"  

/* Handles the arrival of a login message */
int handle_login_message(int fd, char *buffer, int nbytes)
{
#ifdef DEBUG
	printf("handle_login_msg: entering... \n");
	debug_room();
#endif
    
	char *fn = strstr(buffer,NEW_LINE);
	char *end = strstr(fn+1,MSG_END);
	
#ifdef DEBUG
	printf("buffer = @%s@, fn = %p, end = %p\n",buffer,fn,end);
#endif	
	
	/* Consistency checks */
	if(fn == NULL) {
		printf("ill formed message: first_newline is NULL!!!\n");
		send_ill_formed_msg(fd);
		return 1;	
	}
	if(end == NULL) {
		printf("ill formed message: msg_end is NULL!!!\n"); 
		send_ill_formed_msg(fd);
		return 1;	
	}
	if(fn-buffer >= nbytes) {
		printf("ill formed message: new_line exceedes buffer!!!\n");
		send_ill_formed_msg(fd);
		return 1;		
	} 
	if((end + strlen(MSG_END)) - buffer > nbytes) {
		printf("ill formed message: msg_end exceedes buffer!!!\n");
		send_ill_formed_msg(fd);
		return 1;		
	}
	if(end-fn <= 1) {
		printf("ill formed message: msg_end-first_newline <= 1!!!\n"); 
		send_ill_formed_msg(fd);
		return 1;		
	}	
	
	const int nick_len = end-fn-1;
	// creation of an array to hold the nickname
	// with aditional extra space for a trailing zero				
	char nick[nick_len + 1];
	bzero(nick, nick_len + 1);
	
	memcpy(nick, fn + strlen(NEW_LINE), nick_len);
	printf("Nick: %s\n",nick);
	login_user(nick,fd);
    
#ifdef DEBUG
	debug_room();  
	printf("handle_login_msg: leaving... \n");
#endif  
}  

/* Handles the arrival of a logout message */ 
int handle_logout_message(int fd, char *buffer, int nbytes)
{
#ifdef DEBUG
	printf("handle_logout_msg: entering... \n");
	debug_room();
#endif
    
	char *fn = strstr(buffer,NEW_LINE);
	char *end = strstr(fn+1,MSG_END);
	
	/* Consistency checks */
	if(fn == NULL) {
		printf("ill formed message: first_newline is NULL!!!\n");
		send_ill_formed_msg(fd);
		return 1;	
	}
	if(end == NULL) {
		printf("ill formed message: msg_end is NULL!!!\n"); 
		send_ill_formed_msg(fd);
		return 1;	
	}
	if(fn-buffer >= nbytes) {
		printf("ill formed message: new_line exceedes buffer!!!\n");
		send_ill_formed_msg(fd);
		return 1;		
	} 
	if((end + strlen(MSG_END)) - buffer > nbytes) {
		printf("ill formed message: msg_end exceedes buffer!!!\n");
		send_ill_formed_msg(fd);
		return 1;		
	}
	if(end-fn <= 1) {
		printf("ill formed message: msg_end-first_newline > 1!!!\n"); 
		send_ill_formed_msg(fd);
		return 1;		
	}	
	
	const int nick_len = end-fn-1;
	// creation of an array to hold the nickname
	// with aditional extra space for a trailing zero				
	char nick[nick_len + 1];
	bzero(nick, nick_len + 1);
	
	memcpy(nick, fn + strlen(NEW_LINE), nick_len);
	printf("Nick: %s\n",nick);
	logout_user(nick);
    
#ifdef DEBUG
	debug_room();  
	printf("handle_logout_msg: leaving... \n");
#endif	
}

/* Handles the arrival of a msg-post message */ 
int handle_msg_post_message(int fd, char *buffer, int nbytes)
{
//#ifdef DEBUG
	printf("handle_msg_post_msg: entering... \n");
	printf("----------------------------------\n"); 
	printf("@%s@",buffer); 
	printf("----------------------------------\n"); 	
//#endif
    

	char *fn = strstr(buffer,NEW_LINE); 
	char *sn = strstr(fn+1,NEW_LINE);
	
	/* Consistency checks */
	if(fn == NULL) {
		printf("ill formed message: first_newline is NULL!!!\n");
		send_ill_formed_msg(fd);
		return 1;	
	}
	if(sn == NULL) {
		printf("ill formed message: msg_end is NULL!!!\n"); 
		send_ill_formed_msg(fd);
		return 1;	
	}
	if(fn-buffer >= nbytes) {
		printf("ill formed message: new_line exceedes buffer!!!\n");
		send_ill_formed_msg(fd);
		return 1;		
	} 
	if(sn-buffer > nbytes) {
		printf("ill formed message: msg_end exceedes buffer!!!\n");
		send_ill_formed_msg(fd);
		return 1;		
	}
	if(sn-fn <= 1) {
		printf("ill formed message: msg_end-first_newline > 1!!!\n"); 
		send_ill_formed_msg(fd);
		return 1;		
	}	
	
	const int num_len = sn-fn-1;
	// creation of an array to hold the value
	// with aditional extra space for a trailing zero				
	char num[num_len + 1];
	bzero(num, num_len + 1);
	
	memcpy(num, fn + strlen(NEW_LINE), num_len);
	printf("num: %d\n",atoi(num));
	
	int msg_len = atoi(num);
	
	char *end = strstr(sn+1+msg_len,MSG_END);
	if ((sn+1+msg_len) != end) {
		printf("Double enter not present at end of msg\n");
		send_ill_formed_msg(fd);                   
		return 1;
	}  
	
	/* This is intentionally explanatory
	for debbuging purposes. Rewrite without
	unnecessary variables. */
	int header_len = (fn-buffer); //this holds "msg-post"
		
	if(header_len + num_len + msg_len + 2*strlen(NEW_LINE) + strlen(MSG_END) != nbytes) {
		printf("Server-do_service(): Bytes dont sum up!!!! \n\n");  
		send_ill_formed_msg(fd);                        
		return 1;  	
	}
				
	char* message = malloc(msg_len+1);
	bzero(message,msg_len+1);
	memcpy(message,sn + strlen(NEW_LINE),msg_len); 
	
	printf("Message recived = %s",message);
	                     
	fd_set msg_to;
	FD_ZERO(&msg_to); 
	connections_get_logged(&msg_to);
	//connections_all_but(fd,&msg_to,&msg_to);
	send_new_msg_msg(msg_to,user_socktonick(fd),message,msg_len);

	free(message);
	message = NULL;	

    
#ifdef DEBUG       
	printf("handle_msg_post_msg: leaving... \n");
#endif	
}
